import os.path
import random
import tkinter
import tkinter.filedialog
import tkinter.messagebox


number = "1234567890"

def mkdir(path):
    isexist = os.path.exists(path)
    if not isexist:
        os.mkdir(path)


def openfile(filename):
    f = open(filename)
    flist = f.read()
    f.close()
    return flist


def inputbox(showstr, showorder, length):
    instr = input(showstr)
    if len(instr) != 0:
        if showorder == 1:
            if str.isdigit(instr):
                if instr == "0":
                    print("\033[1;31;40m 输入为零，请重新输入！！\033[0m")
                    return "0"
                else:
                    return instr
            else:
                print("\033[1;31;40m输入非法，请重新输入！！\033[0m")
                return "0"

        if showstr == 2:
            if str.isalpha(instr):
                if len(instr) != length:
                    print("\033[1;31;40m必须输入" + str(length) + "个字母，请重新输入！！\033[0m")
                    return "0"
                else:
                    return instr
            else:
                print("\033[1;31;40m输入非法，请重新输入！！\033[0m")

        if showstr == 3:
            if str.isdigit(instr):
                if len(instr) != length:
                    print("\033[1;31;40m必须输入" + str(length) + "个数字，请重新输入！！\033[0m")
                    return "0"
                else:
                    return instr
            else:
                print("\033[1;31;40m输入非法，请重新输入！！\033[0m")
                return "0"
        else:
            print("\033[1;31;40m输入为空，请重新输入！！\033[0m")
            return "0"


def wfile(sstr, sfile, typeis, smsg, datapath, root, randstr):
    mkdir(datapath)
    datafile = datapath + "\\" + sfile
    file = open(datafile, 'w')

    wrlist = sstr
    pdata = ""
    wdata = ""

    for i in range(len(wrlist)):
        wdata = str(wrlist[i].replace("[", ")")).replace("]", ")")
        file.write(str(wdata))
        pdata = pdata + wdata
    file.close()
    print("\033[1;31m" + pdata + "\033[0m")
    if typeis != "no":
        tkinter.messagebox.showinfo("提示", smsg + str(len(randstr)) + "\n 防伪码文件存放位置：" + datafile)
        root.withdraw()


def input_validation(choice):
    if str.isdigit(choice):
        if choice == 0:
            print("\033[1;31;40m 输入非法，请重新输入！！\033[0m")
            return 0
        else:
            return int(choice)
    else:
        print("\033[1;31;40m 输入非法，请重新输入！！\033[0m")
        return 0


def scode1(choice, root, randstr):
    incount = inputbox("\033[1;32m 请输入您要生成防伪码的数量:\33[0m", 1, 0)

    while int(incount) == 0:
        incount = inputbox("\033[1;32m 请输入您要生成防伪码的数量:\33[0m", 1, 0)
    randstr.clear()

    for j in range(int(incount)):
        randfir = ""
        for i in range(6):
            randfir = randfir + random.choice(number)
        randfir = randfir + "\n"
        randstr.append(randfir)

    wfile(randstr, "scode" + str(choice) + ".txt", "", "已生成6位防伪码共计：", "codePath", root, randstr)

def scode2(choice, root, randstr):
    ordstart = inputbox("\033[1;32m 请输入系列产品的数字起始号（3位）:\33[0m", 3, 3)

    while int(ordstart) == 0:
        ordstart = inputbox("\033[1;32m 请输入系列产品的数字起始号（3位）:\33[0m", 1, 0)
    ordcount = inputbox("\033[1;32m 请输入产品系列的数量:", 1, 0)

    while int(ordcount) < 1 or int(ordcount) > 9999:
        ordcount = inputbox("\033[1;32m 请输入产品系列的数量:", 1, 0)
    incount = inputbox("\033[1;32m 请输入要生成的每个系列产品的防伪码数量:\33[0m", 1, 0)
    while int(incount) == 0:
        incount = inputbox("\033[1;32m 请输入您要生成防伪码的数量:\33[0m", 1, 0)

    randstr.clear()
    for m in range(int(ordcount)):
        for j in range(int(incount)):
            randfir = ''
            for i in range(6):
                randfir = randfir + random.choice(number)
            randstr.append(str(int(ordstart) + m) + randfir + "\n")
    wfile(randstr, "scode" + str(choice) + ".txt", "", "已生成9位系列产品防伪码共计：", "codePath", root, randstr)